// StatusForm.cpp : implementation file
//

#include "stdafx.h"
#include "EBASApp.h"
#include "StatusForm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CStatusForm

IMPLEMENT_DYNCREATE(CStatusForm, CFormView)

CStatusForm::CStatusForm()
	: CFormView(CStatusForm::IDD)
{
	//{{AFX_DATA_INIT(CStatusForm)
	m_guid = _T("");
	m_status = _T("");
	//}}AFX_DATA_INIT

	m_pStatus = NULL;
}

CStatusForm::~CStatusForm()
{
}

void CStatusForm::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStatusForm)
	DDX_Control(pDX, IDC_LIST_STATUS, m_list);
	DDX_Text(pDX, IDC_EDIT_GUID, m_guid);
	DDX_Text(pDX, IDC_EDIT_STATUS, m_status);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CStatusForm, CFormView)
	//{{AFX_MSG_MAP(CStatusForm)
	ON_LBN_DBLCLK(IDC_LIST_STATUS, OnDblclkListStatus)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStatusForm diagnostics

#ifdef _DEBUG
void CStatusForm::AssertValid() const
{
	CFormView::AssertValid();
}

void CStatusForm::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CStatusForm message handlers

void CStatusForm::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	try {
		IUnknown * pUnk = NULL; ULONG fetched = 0;
		m_pEnumStatus.CreateInstance(__uuidof(EBASDLLLib::EnumStatus));
		m_pEnumStatus->Next(&pUnk, &fetched);
		while (NULL != pUnk) {
			EBASDLLLib::IStatusPtr pStatus = NULL;
			pUnk->QueryInterface(__uuidof(EBASDLLLib::IStatus), (void**)&pStatus);
			pUnk = NULL;

			m_list.AddString(pStatus->Status);
			pStatus = NULL;

			m_pEnumStatus->Next(&pUnk, &fetched);
		}
	} catch (...) {

	}
	
	UpdateData(FALSE);
}

void CStatusForm::OnRyd() 
{
	UpdateData();

	m_guid = "";
	m_status = "";
	m_pStatus = NULL;

	UpdateData(FALSE);
}

void CStatusForm::OnGem() 
{
	UpdateData();

	try {
		if (NULL == m_pStatus) {
			m_pStatus = CEBASAppApp::gpEbasSession->OpretStatus();
		}
			
		CString navn = (char*)m_pStatus->Status;

		m_pStatus->Status = bstr_t(m_status);
		m_pStatus->SetInfo();

		int pos = m_list.FindString(-1, navn);
		m_list.DeleteString(pos);
		m_list.AddString(m_pStatus->Status);

		m_guid = (char*)m_pStatus->GUID;
		m_status = (char*)m_pStatus->Status;
	} catch (...) {

	}

	UpdateData(FALSE);
}

void CStatusForm::OnSlet() 
{
	UpdateData();

	try {
		if (NULL == m_pStatus)
			return;

		CString navn = (char*)m_pStatus->Status;

		m_pStatus->Slet();

		m_guid = "";
		m_status = "";

		int pos = m_list.FindString(-1, navn);
		m_list.DeleteString(pos);
	} catch (...) {

	}

	UpdateData(FALSE);
}

void CStatusForm::OnDblclkListStatus() 
{
	UpdateData();

	try {
		CString text;
		int sel = m_list.GetCurSel();
		m_list.GetText(sel, text);
		
		m_pStatus = m_pEnumStatus->findStatusNavn(bstr_t(text));
		if (NULL == m_pStatus)
			return;

		m_guid = (char*)m_pStatus->GUID;
		m_status = (char*)m_pStatus->Status;
	} catch (...) {

	}

	UpdateData(FALSE);
}
